layui.extend({
	treeGrid: '{/}/plugins/treeGrid/web/design/extend/treeGrid',
});
layui.define([ 'jquery','treeGrid','form', 'layer','laydate', 'table' ], function(exports) {
	var o = layui.$;
	var form = layui.form;
	var laydate = layui.laydate;
	var table = layui.table;
	var catalogTable = null;
	var ptable = null;
	var layer = layui.layer;
	var treeGrid = layui.treeGrid;//很重要
	var editObj=null,ptable=null,treeGrid=null,tableId='catalogTable',layer=null;
	var param ={
            id:tableId
            ,elem: '#'+tableId
            ,url: '/core/user/catalog/tableTree.json'
            ,cellMinWidth: 100
            ,idField:'id'//必須字段
            ,treeId:'id'//树形id字段名称
            ,treeUpId:'pid'//树形父id字段名称
            ,treeShowName:'name'//以树形式显示的字段
            ,height:'100%'
            ,isFilter:false
            ,iconOpen:true//是否显示图标【默认显示】
            ,isOpenDefault:true//节点默认是展开还是折叠【默认展开】
            ,loading:true
            ,method:'post'
            ,isPage:false
            ,cols: [[
                {
                	field:'name',
                	title: '栏目名称',
                	templet: function(d){
                		var html='<span class="" lay-event="edit">[' + d.id + ']' + d.name  + (d.seq == undefined ? '' : '[' + d.seq + ']') + '</span>';
                        return html;
                    }
                },{
                	title:"操作",
                	templet:"#optionTpl"
                }
            ]]
            ,parseData:function (res) {//数据加载后回调
                return res;
            }
            ,onClickRow:function (index, o) {
                console.log(index,o,"单击！");
            }
            ,onDblClickRow:function (index, o) {
                console.log(index,o,"双击");
            }
            ,onCheck:function (obj,checked,isAll) {//复选事件
                console.log(obj,checked,isAll,"复选");
            }
            ,onRadio:function (obj) {//单选事件
                console.log(obj,"单选");
            }
        };
	var view ={
		
		init:function(){
			layui.link("/plugins/treeGrid/web/design/css/layui.css");//引入treeGrid中需要的css样式，由于跟layui.css可能冲突，在此单独引入
			this.initTable();
			this.initSearchForm();
			this.initToolBar();
			window.dataReload = function(){
				ptable=treeGrid.render(param);
			}
			
		},
		initTable:function(){
			var $=layui.jquery;
	        treeGrid = layui.treeGrid;//很重要
	        layer=layui.layer;
	        ptable=treeGrid.render(param);
	
	    },
		
		initSearchForm:function(){
			Lib.initSearchForm( $("#catalogSearchForm"),catalogTable,form);
		},
		initToolBar:function(){
			toolbar = {
                add : function() { //获取选中数据
                    var url = "/admin/catalog/add.do";
                    //Common.openDlg(url,"栏目管理>新增");
                    $("#rightContent iframe")[0].contentWindow.location.href = url;
                    //Common.openDlg(url,"栏目管理>新增");
                    //$("#rightContent").load(url);
                },
                addSub : function() {
                    var id = $(this).attr("valueId");
                    var url = "/admin/catalog/add.do?cid=" + id;
                    $("#rightContent iframe")[0].contentWindow.location.href = url;
                },
                edit : function() { //获取选中数目
                    var id = $(this).attr("valueId");
                    var url = null;
                    if (id) {
                        url = "/admin/catalog/edit.do?id=" + id;
                    } else {
                        var data = Common.getOneFromTable(table, "articleTable");
                        if (data == null) {
                            return;
                        }
                        url = "/admin/catalog/edit.do?id=" + data.id;
                    }
                    $("#rightContent iframe")[0].contentWindow.location.href = url;
                },
                del : function() {
                    var id = $(this).attr("valueId");
                    layui.use(['del'], function(){
                          var delView = layui.del;
                          delView.delBatch(id);
                    });
                }

			};
			$(document).on("click", ".ext-toolbar", function() {
				var type = $(this).data('type');
				toolbar[type] ? toolbar[type].call(this) : '';
			});
		}
	}
	 exports('index',view);
});

